package com.dlut.txy.designpattern.listener;


import java.util.HashMap;
import java.util.Map;

public abstract class CommonManager<T> {
    protected Map<String, EventListener<T>> listenerMap = new HashMap<>();

    public void addListener(EventListener<T> listener) {
        listenerMap.put(listener.getClass().getName(), listener);
    }

    public void removeListener(EventListener<T> listener) {
        listenerMap.remove(listener.getClass().getName());
    }

    public PublishResult publish(T event) {
        if(this.listenerMap==null || this.listenerMap.size()==0) {
            return PublishResult.buildSuccess();
        }
        for (EventListener<T> eventListener : listenerMap.values()) {
            eventListener.consume(event);
        }
        return PublishResult.buildSuccess();
    }
}
